package artibella.media 
{
	import artibella.StageManager;
	import artibella.IDisposable;
	import artibella.IStageObserver;
	import artibella.loaders.LoaderMedia;
	import artibella.loaders.LoaderMediaParameters;
	
	import caurina.transitions.Tweener;
	
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class ADisplay extends Sprite implements IDisposable, IStageObserver
	{
		protected var _displayMode:String;
		protected var _displayState:String;
		
		protected var _width:int;
		protected var _height:int;
		protected var _ratio:Number;

		protected var _preview:LoaderMedia;
		protected var _previewRatio:Number;
		protected var _hasPreview:Boolean;
		
		protected var _rect:Rectangle;
		protected var _bgColor:int;
		
		
		public function ADisplay( w:int, h:int, bgColor:int = -1, displayMode:String = DisplayMode.FIT ) 
		{
			_width = w;
			_height = h;
			_displayMode = displayMode;
			_bgColor = bgColor;
			
			_hasPreview = false;
			
			_drawBackground( _width, _height );
		}
		
		private function _drawBackground( w:int, h:int ):void
		{
			if ( _bgColor == -1 ) return;
			
			graphics.clear();
			graphics.beginFill( _bgColor, 1 );
			graphics.drawRect( 0, 0, w, h );
			graphics.endFill();
		}
		
		public function get preview():LoaderMedia
		{
			return _preview;
		}
		
		public function get baseWidth():int
		{
			return _width;
		}
		
		public function get baseHeight():int
		{
			return _height;
		}
		
		// - - -
		
		public function loadPreview( url:String  ):void
		{
			_hasPreview = true;
			
			var parameters:LoaderMediaParameters = new LoaderMediaParameters();
			parameters.centered = true;
			parameters.onComplete = _setPreviewRatio;
			
			_preview = new LoaderMedia( url, parameters );
			_preview.x = _width / 2;
			_preview.y = _height / 2;
			_preview.alpha = 0;
			addChildAt( _preview, 0 );

			scrollRect = new Rectangle( 0, 0, _width, _height );
		}
		
		private function _setPreviewRatio():void 
		{
			//_previewRatio = _preview.width / _preview.height; // ?
			_previewRatio = _preview.content.width / _preview.content.height;

			_hasPreview = true;
			
			_resizePreview( _width, _height );
			
			Tweener.addTween( _preview, { alpha:1, time:1 } );
		}
		
		public function get hasPreview():Boolean
		{
			return _hasPreview;
		}
		
		private function _resizePreview( w:int, h:int ):void
		{
			_ratio = w / h;
			
			scrollRect = new Rectangle( 0, 0, w, h );

			switch( _displayMode )
			{
				case DisplayMode.DISTORT:
				
					_preview.width = w;
					_preview.height = h;
					
					break;
				
				case DisplayMode.FIT:
				
					if ( _previewRatio > _ratio )
					{
						_preview.height = h;
						_preview.width = _preview.height * _previewRatio;
					} else {
						
						_preview.width = w;
						_preview.height = _preview.width / _previewRatio;
					}
				
					break;
					
				case DisplayMode.FULL:
				
					if ( _previewRatio > _ratio )
					{
						_preview.width = w;
						_preview.height = _preview.width / _previewRatio;
						
					} else {
						
						_preview.height = h;
						_preview.width = _preview.height * _previewRatio;
					}
				
					break;
					
				case DisplayMode.NORMAL:
				
					// keud
			}
		}
		
		// - - - IStageObserver
		
		public function stageUpdate( w:int, h:int ):void 
		{
			_displayState = StageManager.instance.getStage().displayState;

			if ( _preview != null ) {
			
				switch ( _displayState )
				{
					case StageDisplayState.FULL_SCREEN :
					
						_preview.x = w / 2;
						_preview.y = h / 2;
						
						_resizePreview( w, h );
						
						break;
						
					case StageDisplayState.NORMAL :
					
						_preview.x = _width / 2;
						_preview.y = _height / 2;
						
						_resizePreview( _width, _height );
				}
			}
		}
		
		// - - - IDisposable
		
		public function dispose():void
		{
			if ( _preview != null ) {
				_preview.dispose();
				_preview = null;
			}
		}
	}
}